﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;

namespace Swsk33.SimpleArcEngine.Util
{
	/// <summary>
	/// 画多边形并复制为新图层
	/// </summary>
	public class CopyIntersect : ICommand, ITool
	{
		/// <summary>
		/// 被操作地图对象
		/// </summary>
		private AxMapControl map;

		/// <summary>
		/// 传入图层
		/// </summary>
		private IFeatureLayer inputFeatureLayer;

		public CopyIntersect(IFeatureLayer featureLayer)
		{
			inputFeatureLayer = featureLayer;
		}

		public void OnCreate(object Hook)
		{
			map = (AxMapControl)Hook;
		}

		public bool Enabled
		{
			get
			{
				return true;
			}
		}

		public bool Checked
		{
			get
			{
				return true;
			}
		}

		public string Name
		{
			get;
			set;
		}

		public string Caption
		{
			get;
			set;
		}

		public string Tooltip
		{
			get;
			set;
		}

		public string Message
		{
			get;
			set;
		}

		public string HelpFile
		{
			get;
			set;
		}

		public int HelpContextID
		{
			get;
			set;
		}

		public int Bitmap
		{
			get;
			set;
		}

		public string Category
		{
			get;
			set;
		}

		public void OnClick()
		{

		}

		public void OnMouseDown(int button, int shift, int x, int y)
		{
			// 获取在地图上画的多边形
			IGeometry geometry = map.TrackPolygon();
			// 新建查询器
			ISpatialFilter filter = new SpatialFilter();
			// 设定查询范围为全部字段
			filter.SubFields = "*";
			// 设定查询的几何的范围为我们划定的矩形框范围
			filter.Geometry = geometry;
			// 查询Shape
			filter.GeometryField = "shape";
			// 查询与之相交的要素
			filter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
			// 创建一个拓扑处理用于下面裁切部分
			ITopologicalOperator mainOperator = (ITopologicalOperator)geometry;
			mainOperator.Simplify();
			for (int i = 0; i < map.LayerCount; i++)
			{
				IFeatureLayer layer = (IFeatureLayer)map.get_Layer(i);
				IFeatureCursor cursor = layer.Search(filter, false);
				IFeature feature;
				while ((feature = cursor.NextFeature()) != null)
				{
					IGeometry shape = feature.Shape;
					((ITopologicalOperator)shape).Simplify();
					// 进行相交
					IGeometry getGeometry = mainOperator.Intersect(shape, esriGeometryDimension.esriGeometry2Dimension);
					IFeature newFeature = inputFeatureLayer.FeatureClass.CreateFeature();
					for (int j = 0; j < feature.Fields.FieldCount; j++)
					{
						if (feature.Fields.Field[i].Name.Equals("OID") || feature.Fields.Field[i].Name.Equals("Shape"))
						{
							continue;
						}
						newFeature.Value[i] = feature.Value[i];
					}
					newFeature.Shape = getGeometry;
					newFeature.Store();
				}
			}
			map.Update();
			map.Refresh();
			// 换回默认工具
			map.CurrentTool = null;
		}

		public void OnMouseMove(int button, int shift, int x, int y)
		{

		}

		public void OnMouseUp(int button, int shift, int x, int y)
		{

		}

		public void OnDblClick()
		{

		}

		public void OnKeyDown(int keyCode, int shift)
		{

		}

		public void OnKeyUp(int keyCode, int shift)
		{

		}

		public bool OnContextMenu(int x, int y)
		{
			return true;
		}

		public void Refresh(int hdc)
		{

		}

		public bool Deactivate()
		{
			return true;
		}

		public int Cursor
		{
			get;
			set;
		}
		
	}
}
